package com.LeeCode;

/**
 * 最大子数组和
 */
public class Code053 {
    public static void main(String[] args) {
        int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(new Code053().maxSubArray(nums));
    }

    public int maxSubArray(int[] nums) {
        int maxSum = nums[0], pre = nums[0];
        for (int i = 1; i < nums.length; i++) {
            pre = Math.max(pre + nums[i], nums[i]);
            maxSum = Math.max(maxSum, pre);
        }
        return maxSum;
    }
}
